package com.qingxin.park.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.springframework.stereotype.Service;
import com.qingxin.park.entity.Company;
import com.qingxin.park.entity.SamplingData;
import com.qingxin.sys.dao.Querier;
import com.qingxin.sys.dao.Updater;
import com.qingxin.sys.entity.LoginUser;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.Pager;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.service.BaseService;
import com.qingxin.sys.support.UploadFile;
import com.qingxin.sys.utils.BeanUtil;
import com.qingxin.sys.utils.StringUtils;
/**
 * 抽样检测
 * @author Administrator
 *
 */
@Service
public class SamplingDataService extends BaseService<SamplingData, String>{

	/**
	 * 列表数据
	 * @param search
	 * @param pageParam
	 * @param loginUser
	 * @return
	 */
	public Pager list(String search,PageParam pageParam, LoginUser loginUser){
		Querier querier = Querier.create("from SamplingData where 1=1 ");
		querier.append(pageParam.getOrderby(" order by createTime desc"));
		Pager pager = pageByQuerier(querier, pageParam);
		return pager;
	}
	
	/**
	 * 检测单位 下拉列表数据
	 * @param loginUser
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public List<Company> getJcdwData(LoginUser loginUser){
		List<Company> list = listByHql("select new com.qingxin.jcdw.entity.Jcdw(jcdwid,jcdwname) from Jcdw");
		if(list == null){
			return new ArrayList<Company>();
		}
		return list;
	}
	
	/**
	 * 添加
	 * @param samplingData
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError add(SamplingData samplingData, LoginUser loginUser, HttpServletRequest request){
		samplingData.setCreateTime(new Date());
		if(StringUtils.isNotEmpty(samplingData.getSamplinglist())){
			UploadFile.moveTmpTo(samplingData.getSamplinglist(),UploadFile.DIR_CYJC_ATTACHMENT);
		}
		if(StringUtils.isNotEmpty(samplingData.getExaminingreport())){
			UploadFile.moveTmpTo(samplingData.getSamplinglist(),UploadFile.DIR_CYJC_ATTACHMENT);
		}
		this.save(samplingData);
		return null;
	}
	
	/**
	 * 删除
	 * @param samplingid
	 * @param request
	 * @return
	 */
	public SysError delete(String[] samplingid,HttpServletRequest request){
		for(String id : samplingid){
			SamplingData samplingData = this.get(id);
			if(samplingData == null){
				continue;
			}
			this.delete(samplingData);
		}
		return null;
	}
	
	/**
	 * 修改
	 * @param samplingData
	 * @param loginUser
	 * @param request
	 * @return
	 */
	public SysError update(SamplingData samplingData,LoginUser loginUser, HttpServletRequest request){
		SamplingData old = getSafeEntity(samplingData.getSamplingid());
		if(StringUtils.isNotEmpty(samplingData.getExaminingreport()) && !samplingData.getExaminingreport().equals(old.getExaminingreport())){
			UploadFile.moveTmpTo(samplingData.getExaminingreport(),UploadFile.DIR_CYJC_ATTACHMENT);
		}
		if(StringUtils.isNotEmpty(samplingData.getSamplinglist()) && !samplingData.getSamplinglist().equals(old.getSamplinglist())){
			UploadFile.moveTmpTo(samplingData.getSamplinglist(),UploadFile.DIR_CYJC_ATTACHMENT);
		}
		String fields = "companyid,companyName,jcdwid,jcdwname,samplinglist,examiningreport,samplingnum,source,issueTime";
		BeanUtil.setTargetFields(old,samplingData, fields, true);
		Updater<SamplingData> updater = new Updater<>(old, fields);
		this.update(updater);		
		return null;
	}
}
